Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* AFS Cache Manager Service
0003  *
0004  * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
0005  * Written by David Howells (dhowells@redhat.com)
0006  */
0007 
0008 #include <linux/module.h>
0009 #include <linux/init.h>
0010 #include <linux/slab.h>
0011 #include <linux/sched.h>
0012 #include <linux/ip.h>
0013 #include "internal.h"
0014 #include "afs_cm.h"
0015 #include "protocol_yfs.h"
0016 
0017 static int afs_deliver_cb_init_call_back_state(struct afs_call *);
0018 static int afs_deliver_cb_init_call_back_state3(struct afs_call *);
0019 static int afs_deliver_cb_probe(struct afs_call *);
0020 static int afs_deliver_cb_callback(struct afs_call *);
0021 static int afs_deliver_cb_probe_uuid(struct afs_call *);
0022 static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *);
0023 static void afs_cm_destructor(struct afs_call *);
0024 static void SRXAFSCB_CallBack(struct work_struct *);
0025 static void SRXAFSCB_InitCallBackState(struct work_struct *);
0026 static void SRXAFSCB_Probe(struct work_struct *);
0027 static void SRXAFSCB_ProbeUuid(struct work_struct *);
0028 static void SRXAFSCB_TellMeAboutYourself(struct work_struct *);
0029 
0030 static int afs_deliver_yfs_cb_callback(struct afs_call *);
0031 
0032 /*
0033  * CB.CallBack operation type
0034  */
0035 static const struct afs_call_type afs_SRXCBCallBack = {
0036     .name       = "CB.CallBack",
0037     .deliver    = afs_deliver_cb_callback,
0038     .destructor = afs_cm_destructor,
0039     .work       = SRXAFSCB_CallBack,
0040 };
0041 
0042 /*
0043  * CB.InitCallBackState operation type
0044  */
0045 static const struct afs_call_type afs_SRXCBInitCallBackState = {
0046     .name       = "CB.InitCallBackState",
0047     .deliver    = afs_deliver_cb_init_call_back_state,
0048     .destructor = afs_cm_destructor,
0049     .work       = SRXAFSCB_InitCallBackState,
0050 };
0051 
0052 /*
0053  * CB.InitCallBackState3 operation type
0054  */
0055 static const struct afs_call_type afs_SRXCBInitCallBackState3 = {
0056     .name       = "CB.InitCallBackState3",
0057     .deliver    = afs_deliver_cb_init_call_back_state3,
0058     .destructor = afs_cm_destructor,
0059     .work       = SRXAFSCB_InitCallBackState,
0060 };
0061 
0062 /*
0063  * CB.Probe operation type
0064  */
0065 static const struct afs_call_type afs_SRXCBProbe = {
0066     .name       = "CB.Probe",
0067     .deliver    = afs_deliver_cb_probe,
0068     .destructor = afs_cm_destructor,
0069     .work       = SRXAFSCB_Probe,
0070 };
0071 
0072 /*
0073  * CB.ProbeUuid operation type
0074  */
0075 static const struct afs_call_type afs_SRXCBProbeUuid = {
0076     .name       = "CB.ProbeUuid",
0077     .deliver    = afs_deliver_cb_probe_uuid,
0078     .destructor = afs_cm_destructor,
0079     .work       = SRXAFSCB_ProbeUuid,
0080 };
0081 
0082 /*
0083  * CB.TellMeAboutYourself operation type
0084  */
0085 static const struct afs_call_type afs_SRXCBTellMeAboutYourself = {
0086     .name       = "CB.TellMeAboutYourself",
0087     .deliver    = afs_deliver_cb_tell_me_about_yourself,
0088     .destructor = afs_cm_destructor,
0089     .work       = SRXAFSCB_TellMeAboutYourself,
0090 };
0091 
0092 /*
0093  * YFS CB.CallBack operation type
0094  */
0095 static const struct afs_call_type afs_SRXYFSCB_CallBack = {
0096     .name       = "YFSCB.CallBack",
0097     .deliver    = afs_deliver_yfs_cb_callback,
0098     .destructor = afs_cm_destructor,
0099     .work       = SRXAFSCB_CallBack,
0100 };
0101 
0102 /*
0103  * route an incoming cache manager call
0104  * - return T if supported, F if not
0105  */
0106 bool afs_cm_incoming_call(struct afs_call *call)
0107 {
0108     _enter("{%u, CB.OP %u}", call->service_id, call->operation_ID);
0109 
0110     switch (call->operation_ID) {
0111     case CBCallBack:
0112         call->type = &afs_SRXCBCallBack;
0113         return true;
0114     case CBInitCallBackState:
0115         call->type = &afs_SRXCBInitCallBackState;
0116         return true;
0117     case CBInitCallBackState3:
0118         call->type = &afs_SRXCBInitCallBackState3;
0119         return true;
0120     case CBProbe:
0121         call->type = &afs_SRXCBProbe;
0122         return true;
0123     case CBProbeUuid:
0124         call->type = &afs_SRXCBProbeUuid;
0125         return true;
0126     case CBTellMeAboutYourself:
0127         call->type = &afs_SRXCBTellMeAboutYourself;
0128         return true;
0129     case YFSCBCallBack:
0130         if (call->service_id != YFS_CM_SERVICE)
0131             return false;
0132         call->type = &afs_SRXYFSCB_CallBack;
0133         return true;
0134     default:
0135         return false;
0136     }
0137 }
0138 
0139 /*
0140  * Find the server record by peer address and record a probe to the cache
0141  * manager from a server.
0142  */
0143 static int afs_find_cm_server_by_peer(struct afs_call *call)
0144 {
0145     struct sockaddr_rxrpc srx;
0146     struct afs_server *server;
0147 
0148     rxrpc_kernel_get_peer(call->net->socket, call->rxcall, &srx);
0149 
0150     server = afs_find_server(call->net, &srx);
0151     if (!server) {
0152         trace_afs_cm_no_server(call, &srx);
0153         return 0;
0154     }
0155 
0156     call->server = server;
0157     return 0;
0158 }
0159 
0160 /*
0161  * Find the server record by server UUID and record a probe to the cache
0162  * manager from a server.
0163  */
0164 static int afs_find_cm_server_by_uuid(struct afs_call *call,
0165                       struct afs_uuid *uuid)
0166 {
0167     struct afs_server *server;
0168 
0169     rcu_read_lock();
0170     server = afs_find_server_by_uuid(call->net, call->request);
0171     rcu_read_unlock();
0172     if (!server) {
0173         trace_afs_cm_no_server_u(call, call->request);
0174         return 0;
0175     }
0176 
0177     call->server = server;
0178     return 0;
0179 }
0180 
0181 /*
0182  * Clean up a cache manager call.
0183  */
0184 static void afs_cm_destructor(struct afs_call *call)
0185 {
0186     kfree(call->buffer);
0187     call->buffer = NULL;
0188 }
0189 
0190 /*
0191  * Abort a service call from within an action function.
0192  */
0193 static void afs_abort_service_call(struct afs_call *call, u32 abort_code, int error,
0194                    const char *why)
0195 {
0196     rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
0197                 abort_code, error, why);
0198     afs_set_call_complete(call, error, 0);
0199 }
0200 
0201 /*
0202  * The server supplied a list of callbacks that it wanted to break.
0203  */
0204 static void SRXAFSCB_CallBack(struct work_struct *work)
0205 {
0206     struct afs_call *call = container_of(work, struct afs_call, work);
0207 
0208     _enter("");
0209 
0210     /* We need to break the callbacks before sending the reply as the
0211      * server holds up change visibility till it receives our reply so as
0212      * to maintain cache coherency.
0213      */
0214     if (call->server) {
0215         trace_afs_server(call->server->debug_id,
0216                  refcount_read(&call->server->ref),
0217                  atomic_read(&call->server->active),
0218                  afs_server_trace_callback);
0219         afs_break_callbacks(call->server, call->count, call->request);
0220     }
0221 
0222     afs_send_empty_reply(call);
0223     afs_put_call(call);
0224     _leave("");
0225 }
0226 
0227 /*
0228  * deliver request data to a CB.CallBack call
0229  */
0230 static int afs_deliver_cb_callback(struct afs_call *call)
0231 {
0232     struct afs_callback_break *cb;
0233     __be32 *bp;
0234     int ret, loop;
0235 
0236     _enter("{%u}", call->unmarshall);
0237 
0238     switch (call->unmarshall) {
0239     case 0:
0240         afs_extract_to_tmp(call);
0241         call->unmarshall++;
0242 
0243         /* extract the FID array and its count in two steps */
0244         fallthrough;
0245     case 1:
0246         _debug("extract FID count");
0247         ret = afs_extract_data(call, true);
0248         if (ret < 0)
0249             return ret;
0250 
0251         call->count = ntohl(call->tmp);
0252         _debug("FID count: %u", call->count);
0253         if (call->count > AFSCBMAX)
0254             return afs_protocol_error(call, afs_eproto_cb_fid_count);
0255 
0256         call->buffer = kmalloc(array3_size(call->count, 3, 4),
0257                        GFP_KERNEL);
0258         if (!call->buffer)
0259             return -ENOMEM;
0260         afs_extract_to_buf(call, call->count * 3 * 4);
0261         call->unmarshall++;
0262 
0263         fallthrough;
0264     case 2:
0265         _debug("extract FID array");
0266         ret = afs_extract_data(call, true);
0267         if (ret < 0)
0268             return ret;
0269 
0270         _debug("unmarshall FID array");
0271         call->request = kcalloc(call->count,
0272                     sizeof(struct afs_callback_break),
0273                     GFP_KERNEL);
0274         if (!call->request)
0275             return -ENOMEM;
0276 
0277         cb = call->request;
0278         bp = call->buffer;
0279         for (loop = call->count; loop > 0; loop--, cb++) {
0280             cb->fid.vid = ntohl(*bp++);
0281             cb->fid.vnode   = ntohl(*bp++);
0282             cb->fid.unique  = ntohl(*bp++);
0283         }
0284 
0285         afs_extract_to_tmp(call);
0286         call->unmarshall++;
0287 
0288         /* extract the callback array and its count in two steps */
0289         fallthrough;
0290     case 3:
0291         _debug("extract CB count");
0292         ret = afs_extract_data(call, true);
0293         if (ret < 0)
0294             return ret;
0295 
0296         call->count2 = ntohl(call->tmp);
0297         _debug("CB count: %u", call->count2);
0298         if (call->count2 != call->count && call->count2 != 0)
0299             return afs_protocol_error(call, afs_eproto_cb_count);
0300         call->iter = &call->def_iter;
0301         iov_iter_discard(&call->def_iter, READ, call->count2 * 3 * 4);
0302         call->unmarshall++;
0303 
0304         fallthrough;
0305     case 4:
0306         _debug("extract discard %zu/%u",
0307                iov_iter_count(call->iter), call->count2 * 3 * 4);
0308 
0309         ret = afs_extract_data(call, false);
0310         if (ret < 0)
0311             return ret;
0312 
0313         call->unmarshall++;
0314         fallthrough;
0315 
0316     case 5:
0317         break;
0318     }
0319 
0320     if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
0321         return afs_io_error(call, afs_io_error_cm_reply);
0322 
0323     /* we'll need the file server record as that tells us which set of
0324      * vnodes to operate upon */
0325     return afs_find_cm_server_by_peer(call);
0326 }
0327 
0328 /*
0329  * allow the fileserver to request callback state (re-)initialisation
0330  */
0331 static void SRXAFSCB_InitCallBackState(struct work_struct *work)
0332 {
0333     struct afs_call *call = container_of(work, struct afs_call, work);
0334 
0335     _enter("{%p}", call->server);
0336 
0337     if (call->server)
0338         afs_init_callback_state(call->server);
0339     afs_send_empty_reply(call);
0340     afs_put_call(call);
0341     _leave("");
0342 }
0343 
0344 /*
0345  * deliver request data to a CB.InitCallBackState call
0346  */
0347 static int afs_deliver_cb_init_call_back_state(struct afs_call *call)
0348 {
0349     int ret;
0350 
0351     _enter("");
0352 
0353     afs_extract_discard(call, 0);
0354     ret = afs_extract_data(call, false);
0355     if (ret < 0)
0356         return ret;
0357 
0358     /* we'll need the file server record as that tells us which set of
0359      * vnodes to operate upon */
0360     return afs_find_cm_server_by_peer(call);
0361 }
0362 
0363 /*
0364  * deliver request data to a CB.InitCallBackState3 call
0365  */
0366 static int afs_deliver_cb_init_call_back_state3(struct afs_call *call)
0367 {
0368     struct afs_uuid *r;
0369     unsigned loop;
0370     __be32 *b;
0371     int ret;
0372 
0373     _enter("");
0374 
0375     _enter("{%u}", call->unmarshall);
0376 
0377     switch (call->unmarshall) {
0378     case 0:
0379         call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
0380         if (!call->buffer)
0381             return -ENOMEM;
0382         afs_extract_to_buf(call, 11 * sizeof(__be32));
0383         call->unmarshall++;
0384 
0385         fallthrough;
0386     case 1:
0387         _debug("extract UUID");
0388         ret = afs_extract_data(call, false);
0389         switch (ret) {
0390         case 0:     break;
0391         case -EAGAIN:   return 0;
0392         default:    return ret;
0393         }
0394 
0395         _debug("unmarshall UUID");
0396         call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
0397         if (!call->request)
0398             return -ENOMEM;
0399 
0400         b = call->buffer;
0401         r = call->request;
0402         r->time_low         = b[0];
0403         r->time_mid         = htons(ntohl(b[1]));
0404         r->time_hi_and_version      = htons(ntohl(b[2]));
0405         r->clock_seq_hi_and_reserved    = ntohl(b[3]);
0406         r->clock_seq_low        = ntohl(b[4]);
0407 
0408         for (loop = 0; loop < 6; loop++)
0409             r->node[loop] = ntohl(b[loop + 5]);
0410 
0411         call->unmarshall++;
0412         fallthrough;
0413 
0414     case 2:
0415         break;
0416     }
0417 
0418     if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
0419         return afs_io_error(call, afs_io_error_cm_reply);
0420 
0421     /* we'll need the file server record as that tells us which set of
0422      * vnodes to operate upon */
0423     return afs_find_cm_server_by_uuid(call, call->request);
0424 }
0425 
0426 /*
0427  * allow the fileserver to see if the cache manager is still alive
0428  */
0429 static void SRXAFSCB_Probe(struct work_struct *work)
0430 {
0431     struct afs_call *call = container_of(work, struct afs_call, work);
0432 
0433     _enter("");
0434     afs_send_empty_reply(call);
0435     afs_put_call(call);
0436     _leave("");
0437 }
0438 
0439 /*
0440  * deliver request data to a CB.Probe call
0441  */
0442 static int afs_deliver_cb_probe(struct afs_call *call)
0443 {
0444     int ret;
0445 
0446     _enter("");
0447 
0448     afs_extract_discard(call, 0);
0449     ret = afs_extract_data(call, false);
0450     if (ret < 0)
0451         return ret;
0452 
0453     if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
0454         return afs_io_error(call, afs_io_error_cm_reply);
0455     return afs_find_cm_server_by_peer(call);
0456 }
0457 
0458 /*
0459  * Allow the fileserver to quickly find out if the cache manager has been
0460  * rebooted.
0461  */
0462 static void SRXAFSCB_ProbeUuid(struct work_struct *work)
0463 {
0464     struct afs_call *call = container_of(work, struct afs_call, work);
0465     struct afs_uuid *r = call->request;
0466 
0467     _enter("");
0468 
0469     if (memcmp(r, &call->net->uuid, sizeof(call->net->uuid)) == 0)
0470         afs_send_empty_reply(call);
0471     else
0472         afs_abort_service_call(call, 1, 1, "K-1");
0473 
0474     afs_put_call(call);
0475     _leave("");
0476 }
0477 
0478 /*
0479  * deliver request data to a CB.ProbeUuid call
0480  */
0481 static int afs_deliver_cb_probe_uuid(struct afs_call *call)
0482 {
0483     struct afs_uuid *r;
0484     unsigned loop;
0485     __be32 *b;
0486     int ret;
0487 
0488     _enter("{%u}", call->unmarshall);
0489 
0490     switch (call->unmarshall) {
0491     case 0:
0492         call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
0493         if (!call->buffer)
0494             return -ENOMEM;
0495         afs_extract_to_buf(call, 11 * sizeof(__be32));
0496         call->unmarshall++;
0497 
0498         fallthrough;
0499     case 1:
0500         _debug("extract UUID");
0501         ret = afs_extract_data(call, false);
0502         switch (ret) {
0503         case 0:     break;
0504         case -EAGAIN:   return 0;
0505         default:    return ret;
0506         }
0507 
0508         _debug("unmarshall UUID");
0509         call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
0510         if (!call->request)
0511             return -ENOMEM;
0512 
0513         b = call->buffer;
0514         r = call->request;
0515         r->time_low         = b[0];
0516         r->time_mid         = htons(ntohl(b[1]));
0517         r->time_hi_and_version      = htons(ntohl(b[2]));
0518         r->clock_seq_hi_and_reserved    = ntohl(b[3]);
0519         r->clock_seq_low        = ntohl(b[4]);
0520 
0521         for (loop = 0; loop < 6; loop++)
0522             r->node[loop] = ntohl(b[loop + 5]);
0523 
0524         call->unmarshall++;
0525         fallthrough;
0526 
0527     case 2:
0528         break;
0529     }
0530 
0531     if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
0532         return afs_io_error(call, afs_io_error_cm_reply);
0533     return afs_find_cm_server_by_peer(call);
0534 }
0535 
0536 /*
0537  * allow the fileserver to ask about the cache manager's capabilities
0538  */
0539 static void SRXAFSCB_TellMeAboutYourself(struct work_struct *work)
0540 {
0541     struct afs_call *call = container_of(work, struct afs_call, work);
0542     int loop;
0543 
0544     struct {
0545         struct /* InterfaceAddr */ {
0546             __be32 nifs;
0547             __be32 uuid[11];
0548             __be32 ifaddr[32];
0549             __be32 netmask[32];
0550             __be32 mtu[32];
0551         } ia;
0552         struct /* Capabilities */ {
0553             __be32 capcount;
0554             __be32 caps[1];
0555         } cap;
0556     } reply;
0557 
0558     _enter("");
0559 
0560     memset(&reply, 0, sizeof(reply));
0561 
0562     reply.ia.uuid[0] = call->net->uuid.time_low;
0563     reply.ia.uuid[1] = htonl(ntohs(call->net->uuid.time_mid));
0564     reply.ia.uuid[2] = htonl(ntohs(call->net->uuid.time_hi_and_version));
0565     reply.ia.uuid[3] = htonl((s8) call->net->uuid.clock_seq_hi_and_reserved);
0566     reply.ia.uuid[4] = htonl((s8) call->net->uuid.clock_seq_low);
0567     for (loop = 0; loop < 6; loop++)
0568         reply.ia.uuid[loop + 5] = htonl((s8) call->net->uuid.node[loop]);
0569 
0570     reply.cap.capcount = htonl(1);
0571     reply.cap.caps[0] = htonl(AFS_CAP_ERROR_TRANSLATION);
0572     afs_send_simple_reply(call, &reply, sizeof(reply));
0573     afs_put_call(call);
0574     _leave("");
0575 }
0576 
0577 /*
0578  * deliver request data to a CB.TellMeAboutYourself call
0579  */
0580 static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *call)
0581 {
0582     int ret;
0583 
0584     _enter("");
0585 
0586     afs_extract_discard(call, 0);
0587     ret = afs_extract_data(call, false);
0588     if (ret < 0)
0589         return ret;
0590 
0591     if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
0592         return afs_io_error(call, afs_io_error_cm_reply);
0593     return afs_find_cm_server_by_peer(call);
0594 }
0595 
0596 /*
0597  * deliver request data to a YFS CB.CallBack call
0598  */
0599 static int afs_deliver_yfs_cb_callback(struct afs_call *call)
0600 {
0601     struct afs_callback_break *cb;
0602     struct yfs_xdr_YFSFid *bp;
0603     size_t size;
0604     int ret, loop;
0605 
0606     _enter("{%u}", call->unmarshall);
0607 
0608     switch (call->unmarshall) {
0609     case 0:
0610         afs_extract_to_tmp(call);
0611         call->unmarshall++;
0612 
0613         /* extract the FID array and its count in two steps */
0614         fallthrough;
0615     case 1:
0616         _debug("extract FID count");
0617         ret = afs_extract_data(call, true);
0618         if (ret < 0)
0619             return ret;
0620 
0621         call->count = ntohl(call->tmp);
0622         _debug("FID count: %u", call->count);
0623         if (call->count > YFSCBMAX)
0624             return afs_protocol_error(call, afs_eproto_cb_fid_count);
0625 
0626         size = array_size(call->count, sizeof(struct yfs_xdr_YFSFid));
0627         call->buffer = kmalloc(size, GFP_KERNEL);
0628         if (!call->buffer)
0629             return -ENOMEM;
0630         afs_extract_to_buf(call, size);
0631         call->unmarshall++;
0632 
0633         fallthrough;
0634     case 2:
0635         _debug("extract FID array");
0636         ret = afs_extract_data(call, false);
0637         if (ret < 0)
0638             return ret;
0639 
0640         _debug("unmarshall FID array");
0641         call->request = kcalloc(call->count,
0642                     sizeof(struct afs_callback_break),
0643                     GFP_KERNEL);
0644         if (!call->request)
0645             return -ENOMEM;
0646 
0647         cb = call->request;
0648         bp = call->buffer;
0649         for (loop = call->count; loop > 0; loop--, cb++) {
0650             cb->fid.vid = xdr_to_u64(bp->volume);
0651             cb->fid.vnode   = xdr_to_u64(bp->vnode.lo);
0652             cb->fid.vnode_hi = ntohl(bp->vnode.hi);
0653             cb->fid.unique  = ntohl(bp->vnode.unique);
0654             bp++;
0655         }
0656 
0657         afs_extract_to_tmp(call);
0658         call->unmarshall++;
0659         fallthrough;
0660 
0661     case 3:
0662         break;
0663     }
0664 
0665     if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
0666         return afs_io_error(call, afs_io_error_cm_reply);
0667 
0668     /* We'll need the file server record as that tells us which set of
0669      * vnodes to operate upon.
0670      */
0671     return afs_find_cm_server_by_peer(call);
0672 }