]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/operational.c
More for #5860 - if the cache blew past the maxsize, bring it all the way
[openldap] / servers / slapd / back-bdb / operational.c
1 /* operational.c - bdb backend operational attributes function */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2009 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/string.h>
22 #include <ac/socket.h>
23
24 #include "slap.h"
25 #include "back-bdb.h"
26
27 /*
28  * sets *hasSubordinates to LDAP_COMPARE_TRUE/LDAP_COMPARE_FALSE
29  * if the entry has children or not.
30  */
31 int
32 bdb_hasSubordinates(
33         Operation       *op,
34         Entry           *e,
35         int             *hasSubordinates )
36 {
37         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
38         struct bdb_op_info      *opinfo;
39         OpExtra *oex;
40         DB_TXN          *rtxn;
41         int             rc;
42         
43         assert( e != NULL );
44
45         /* NOTE: this should never happen, but it actually happens
46          * when using back-relay; until we find a better way to
47          * preserve entry's private information while rewriting it,
48          * let's disable the hasSubordinate feature for back-relay.
49          */
50         if ( BEI( e ) == NULL ) {
51                 return LDAP_OTHER;
52         }
53
54         /* Check for a txn in a parent op, otherwise use reader txn */
55         LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
56                 if ( oex->oe_key == bdb )
57                         break;
58         }
59         opinfo = (struct bdb_op_info *) oex;
60         if ( opinfo && opinfo->boi_txn ) {
61                 rtxn = opinfo->boi_txn;
62         } else {
63                 rc = bdb_reader_get(op, bdb->bi_dbenv, &rtxn);
64                 if ( rc ) return LDAP_OTHER;
65         }
66
67 retry:
68         /* FIXME: we can no longer assume the entry's e_private
69          * field is correctly populated; so we need to reacquire
70          * it with reader lock */
71         rc = bdb_cache_children( op, rtxn, e );
72
73         switch( rc ) {
74         case DB_LOCK_DEADLOCK:
75         case DB_LOCK_NOTGRANTED:
76                 goto retry;
77
78         case 0:
79                 *hasSubordinates = LDAP_COMPARE_TRUE;
80                 break;
81
82         case DB_NOTFOUND:
83                 *hasSubordinates = LDAP_COMPARE_FALSE;
84                 rc = LDAP_SUCCESS;
85                 break;
86
87         default:
88                 Debug(LDAP_DEBUG_ARGS, 
89                         "<=- " LDAP_XSTRING(bdb_hasSubordinates)
90                         ": has_children failed: %s (%d)\n", 
91                         db_strerror(rc), rc, 0 );
92                 rc = LDAP_OTHER;
93         }
94
95         return rc;
96 }
97
98 /*
99  * sets the supported operational attributes (if required)
100  */
101 int
102 bdb_operational(
103         Operation       *op,
104         SlapReply       *rs )
105 {
106         Attribute       **ap;
107
108         assert( rs->sr_entry != NULL );
109
110         for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
111                 /* just count */ ;
112
113         if ( SLAP_OPATTRS( rs->sr_attr_flags ) ||
114                         ad_inlist( slap_schema.si_ad_hasSubordinates, rs->sr_attrs ) )
115         {
116                 int     hasSubordinates, rc;
117
118                 rc = bdb_hasSubordinates( op, rs->sr_entry, &hasSubordinates );
119                 if ( rc == LDAP_SUCCESS ) {
120                         *ap = slap_operational_hasSubordinate( hasSubordinates == LDAP_COMPARE_TRUE );
121                         assert( *ap != NULL );
122
123                         ap = &(*ap)->a_next;
124                 }
125         }
126
127         return LDAP_SUCCESS;
128 }
129